package cn.usian.service.impl;

import cn.usian.common.R;

import cn.usian.entity.Category;
import cn.usian.entity.Dish;
import cn.usian.entity.Employee;
import cn.usian.entity.Setmeal;
import cn.usian.exception.CusromerException;
import cn.usian.exception.NameException;
import cn.usian.mapper.CategortMapper;
import cn.usian.mapper.DishMapper;
import cn.usian.mapper.SetmealMapper;
import cn.usian.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Resource
    private CategortMapper categortMapper;
    @Resource
    private DishMapper dishMapper;
    @Resource
    private SetmealMapper setmealMapper;
    @Override
    public R<Page> page(int page, int pageSize, String name) {
        LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<>();
        Page<Category> page1 = new Page<>(page,10);
        Page<Category> employeePage = categortMapper.selectPage(page1, queryWrapper);
        return R.success(employeePage);
    }

    @Override
    public void save(Category category) {
//        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
       categortMapper.insert(category);
    }

    @Override
    public void del(Long id) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId,id);
        List<Dish> dishList = dishMapper.selectList(queryWrapper);
        if (!dishList.isEmpty()){
            throw  new CusromerException("该分类已关联其他菜品不能删除");
        }
        LambdaQueryWrapper<Setmeal> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Setmeal::getCategoryId,id);
        List<Setmeal> setmealList = setmealMapper.selectList(queryWrapper1);
        if (!setmealList.isEmpty()){
            throw  new CusromerException("该分类已关联其他菜品不能删除");
        }
        categortMapper.deleteById(id);
    }

    @Override
    public void updateEmp(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        LambdaUpdateWrapper<Category> updateWrapper = new LambdaUpdateWrapper<>();
        if (category.getName()!=null&&category.equals("")){
            updateWrapper.set(Category::getName,category.getName());
        }
        if (category.getType()!=null&&category.equals("")){
            updateWrapper.set(Category::getType,category.getType());
        }
        if (category.getSort()!=null&&category.equals("")){
            updateWrapper.set(Category::getSort,category.getSort());
        }
        categortMapper.updateById(category);
    }

    @Override
    public List<Category> findAll() {
        return categortMapper.selectList(null);
    }

    @Override
    public R<List<Category>> findByList(Integer type) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getType,type);
        List<Category> list=categortMapper.selectList(queryWrapper);
        return R.success(list);
    }


}
